#!/bin/sh

echo "备份开始时间："
date "+%F %T"
echo

DB_BKPATH='/home/docker/data/mysql-main-backup'
DATE=$(date +%Y%m%d)
DATETIME=$(date +%Y%m%d%H%M)
MONTH_AGO=$(date -d "-2 month" +%Y%m)

dbarr=(
    'fun-boot password fun-boot'
)

cd ${DB_BKPATH}

mkdir ${DATETIME}

for i in "${dbarr[@]}"; do

    db=($i)

    db_user=${db[0]}
    db_pass=${db[1]}
    db_name=${db[2]}

    db_user=root
    db_pass=12345678

    echo "$db_name Start mysqldump ……"

    # 系统安装部署MySQL备份
    #/usr/bin/mysqldump --opt -u${db_user} -p${db_pass} ${DB_NAME} > ${DB_BKPATH}/${DATETIME}/${db_name}_${DATETIME}.sql

    # Docker容器部署MySQL备份
    docker exec -i 2fd0178e80f6 mysqldump -u${db_user} -p${db_pass} --set-gtid-purged=OFF ${db_name} | gzip >${DB_BKPATH}/${DATETIME}/${db_name}_${DATETIME}.sql.gz

    echo "$db_name Complete mysqldump"

done

echo "压缩: ${DB_BKPATH}/${DATETIME}"
tar -czvf ${DATETIME}.tar.gz ${DATETIME}

/home/docker/data/mysql-main-backup/mc cp ${DATETIME}.tar.gz minio3233/backup2022/${DATE}/${DATETIME}-db-funboot-233.tar.gz

rm -rf ${DB_BKPATH}/${DATETIME}

echo "删除一个月之前的备份：${DB_BKPATH}/${MONTH_AGO}*"
rm -rf ${DB_BKPATH}/${MONTH_AGO}*

echo
echo "备份完成时间："
date "+%F %T"
echo
